<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Frontend.php 1422 2011-06-27 06:14:59Z chen.hao $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Default action for frontend
 * 
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Frontend.php 1422 2011-06-27 06:14:59Z chen.hao $$
 */
class Evebit_Controller_Action_Frontend extends Evebit_Controller_Action_Public {
	
	
	/**
	 * filter no authentication user
	 */
	public function preDispatch() {
		/*$scope = $this->getInvokeArg('bootstrap')->getOption('scope');
		$module = $this->getRequest()->getModuleName();
		$controller = $this->getRequest()->getControllerName();
	    $action = $this->getRequest()->getActionName();
		
		//$allowActionParam means all item or just self id item or other range
		if (!Zend_Registry::isRegistered('resource')) {
			$resource = new Default_Model_Entity_Resource();
			$collection = $resource->getCollection();
			Zend_Registry::set('resource', $collection);
		}
		
		$collection = Zend_Registry::get('resource');
		$resource = new Default_Model_Entity_Resource();
		$items = $collection->getItems();
		$isExitController = false;
		$isExitParams = false;
		foreach ($items as $item) {
			if ($item->getScope() == $scope && $item->getModule() == $module && $item->getController() == $controller && $item->getAction() == $action) {
				$isExitController = true;
				break;
			}
		}
		if (!$isExitController) {
			if ($isExitParams) {
				$collection = $resource->getCollection();
				$collection->addFilter("controller", $controller);
				$collection->addFilter("action", action);
			}
			$resource->setData("controller", $controller);
			$resource->setData("action", $action);
			$paramsValue = "";
			$resource->setData("params", $paramsValue);
			$resource->setData("scope", $this->getInvokeArg('bootstrap')->getOption('scope'));
			$resource->setData("module", $module);
			$resource->save();
		}
		// autoAdd resource function end
		
		$skipPages = array(
		        array('default','index','login'),array('default','index','logout'),
				array('default','index','index'),array('default','index','gallary'),
				array('default','article','index'),array('gmail','index','contact-us'),
				array('default','index','show-login'),array('default','index','main')
		);*/
		//$this->preCheck($skipPages);
		
	    $module = $this->getRequest ()->getModuleName ();
		$controller = $this->getRequest ()->getControllerName ();
		$action = $this->getRequest ()->getActionName ();
		if ($action == 'img1'){
			echo $_SERVER['REMOTE_ADDR'];
			exit;
		}
		$params = $this->getRequest ()->getParams ();
		
		if (! Zend_Registry::isRegistered ( 'resource' )) {
			$resource = new Default_Model_Entity_Resource ();
			$collection = $resource->getCollection ();
			Zend_Registry::set ( 'resource', $collection );
		}
		$collection = Zend_Registry::get ( 'resource' );
		$items = $collection->getItems ();
		
		$isExitController = false;
		$scope = $this->getInvokeArg ( 'bootstrap' )->getOption ( 'scope' );
		foreach ( $items as $item ) {
			if ($item->getModule () == $module 
			&& $item->getController () == $controller 
			&& $item->getAction () == $action
			&& $item->getScope() == $scope) {
				$isExitController = true;
				break;
			}
		}
		if (! $isExitController) {
			$resource = new Default_Model_Entity_Resource ();
			$resource->setData ( "controller", $controller );
			$resource->setData ( "action", $action );
			$resource->setData ( "scope", $scope );
			$resource->setData ( "module", $module );
			$resource->setData ( "params", '' );
			$resource->save ();
		}
		
		$user = $this->getUser ();
		if (! $user->checkAcl ( $this )) {
			$this->getSession ()->addError ( $this->__ ( "You don't have permission to view this page!" ) );
			$configEntity = new Default_Model_Entity_Config ();$this->_redirect ($configEntity->getCodeValue ( 'wp_base_url' )); ;
		}
	}
	
	/**
	 * check user login
	 */
	public function preCheck($skipPages = array()) {
		$module = $this->getRequest()->getModuleName();
		$controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName ();
		
		$skipArr = array($module,$controller,$action);
		//$skipModule = array ('information','planner');
		$skipModule = array ('information');
		
	    if (!in_array($skipArr, $skipPages) && !in_array($module,$skipModule)) {
		    /*if ($module != 'planner') {
		    	echo $module;
		    	echo '<br/>';
		    	echo $controller;
		    	echo '<br/>';
		    	echo $action;
		    	echo '<br/>';
		    	print_r($skipPages);
		    	exit;
		    }*/
	    	if (! Zend_Auth::getInstance ()->hasIdentity () ) {
	    		$this->getSession()->addError($this->__('You need log in'));
			    $this->_redirect('/default/index/index');
	    	} else {
	    	    //auto acl check start
				$user = $this->getUser();
				if (!$user->checkAcl($this)) {
					$this->getSession()->addError(
						$this->__("You don't have permission to view this page!"));
					$this->_redirect("/default/index/index");
				}
	    	}
		}
	}
	
}
